using UnityEngine;
using System.Collections;

public class SkinnyController : MonoBehaviour {
	
	public float walkSpeed = 10f;
	public float runSpeed = 20f;
	public float initialSpeed;
	public float jumpSpeed = 30f;
	public float gravity = 50f;
	
	public bool running = false;
	//public bool areYouAFatty = false;

	private CharacterController characterController;

	public GameObject fatGuy;
	public GameObject skinnyGuy;
	
	private Vector3 moveDirection;

	// Use this for initialization
	void Start () {
		characterController = GetComponent <CharacterController>();
	}
	
	// Update is called once per frame
	void Update () {
		
//		if (Input.GetKeyDown(KeyCode.LeftControl))
//		{
//			Messenger.Broadcast("Threw up");
//			//areYouAFatty = false;
//			//gravity = 50f;
//		}
		
		if (Input.GetKeyDown(KeyCode.LeftShift))
		{
			running = true;
		}

		if (Input.GetKeyUp(KeyCode.LeftShift))
		{
			running = false;
		}
	
		if (characterController.isGrounded)
		{
			moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
			moveDirection = transform.TransformDirection(moveDirection);
			if (running)
			{
				moveDirection *= runSpeed;
			}
			else
			{
				moveDirection *= walkSpeed;
			}
			
			Debug.Log (moveDirection);
			
			// hackish, but it works. this is just to check if we're moving at all. should be different for if in air
			if (moveDirection.magnitude > 0)
			{
				animation.CrossFade("BasicWalk");
				//Debug.Log ("Moving with basic walk");
			}
			else
			{
				//Debug.Log ("Idle animation");
				animation.CrossFade ("BasicIdle");
			}
			
			//transform.Rotate(transform.InverseTransformDirection(moveDirection));

			if (Input.GetKeyDown(KeyCode.Space))
			{
				moveDirection.y = jumpSpeed;
				initialSpeed = walkSpeed;
				if (running)
				{
					initialSpeed += runSpeed;
				}
			}
		}
		else
		{
			moveDirection += new Vector3(Input.GetAxis("Horizontal")/10,0,Input.GetAxis("Vertical")/10);
			moveDirection.x = Mathf.Clamp(moveDirection.x, -initialSpeed, initialSpeed);
			moveDirection.z = Mathf.Clamp(moveDirection.z, -initialSpeed, initialSpeed);
		}

		moveDirection.y -= gravity * Time.deltaTime;
		characterController.Move(moveDirection * Time.deltaTime);
	}
	
	void OnTriggerEnter(Collider other)
	{
		if (other.gameObject.tag == "Food")
		{
			//running = false;
			//gravity = 200f;
			//areYouAFatty = true;
			Messenger.Broadcast("Ate food");
			DestroyObject (other.gameObject);
		}
	}	
}
